package qc.module.sl.api.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import qc.module.sl.api.ServiceCall;
import qc.module.sl.dto.station.StationDto;
import qc.module.sl.dto.station.StationQueryConditionDto;
import qc.module.sl.dto.station.classify.StationClassifyDto;
import qc.module.sl.dto.station.classify.StationClassifyQueryConditionDto;
import qc.module.sl.service.BasicStationClassifyService;
import qc.module.sl.service.BasicStationsService;

import java.util.List;
import java.util.Map;

/**
 * StationController
 *
 * @author QuCheng Tech
 * @create 2023/1/9
 */
@RestController
@RequestMapping("/station")
public class StationController {
    private BasicStationsService basicStationsService;
    private BasicStationClassifyService basicStationClassifyService;

    @Autowired
    public void setBasicStationsService(BasicStationsService basicStationsService) {
        this.basicStationsService = basicStationsService;
    }

    @Autowired
    public void setBasicStationClassifyService(BasicStationClassifyService basicStationClassifyService) {
        this.basicStationClassifyService = basicStationClassifyService;
    }

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    /**
     * 根据查询条件查询站点分类信息
     *
     * @param dto 站点分类查询dto
     * @return 站点分类信息集合
     * @author QcCheng Tech
     * @since 2023/2/15
     */
    @GetMapping("/queryclassify")
    public List<StationClassifyDto> queryClassify(@RequestBody StationClassifyQueryConditionDto dto) {
        return basicStationClassifyService.query(dto);
    }

    /**
     * 根据查询条件查询站点信息
     *
     * @return 站点信息集合
     * @param    dto    站点信息查询条件dto
     * @author QcCheng Tech
     * @since 2023/2/15
     */
    @GetMapping("/query")
    public List<StationDto> query(@RequestBody StationQueryConditionDto dto) {
        List<StationDto> stationDtoList = basicStationsService.query(dto);
        try {
            if (stationDtoList != null) {
                ServiceCall serviceCall = new ServiceCall();

                Map<String, String> adminDivisionMap = serviceCall.getAdminDivision(restTemplate, discoveryClient);

                //查询到行政区划信息，对adname字段赋值
                if (adminDivisionMap != null) {
                    for (int i = 0; i < stationDtoList.size(); i++) {
                        for (Map.Entry<String, String> entry : adminDivisionMap.entrySet()) {
                            if (!StringUtils.isBlank(stationDtoList.get(i).getAdcode()) && stationDtoList.get(i).getAdcode().equals(entry.getKey())) {
                                stationDtoList.get(i).setAdname(entry.getValue());
                                break;
                            }
                        }
                    }
                }
            }

            return stationDtoList;
        } catch (Exception exception) {
            return stationDtoList;
        }
    }
}
